package com.nidu.demo.mail.model;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseEntity;
import com.nidu.demo.base.BaseStatusEntity;
import com.nidu.demo.common.constants.RegexPatternConstants;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.Map;

/**
 * 邮件模板领域实体
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class MailTemplate extends BaseEntity implements BaseStatusEntity, BaseDeletableEntity {

    private Long id;

    /**
     * 发送的邮箱账号编号
     */
    private Long accountId;

    /**
     * 模板名称
     */
    private String templateName;

    /**
     * 模板编码
     */
    private String templateCode;

    /**
     * 发送人名称
     */
    private String templateNickname;

    /**
     * 模板标题
     */
    private String templateTitle;

    /**
     * 模板内容
     */
    private String templateContent;

    /**
     * 参数数组
     */
    private List<String> templateParams;

    /**
     * 备注
     */
    private String remark;

    /**
     * 开启状态
     */
    private Integer status;

    /**
     * 是否删除
     */
    private Boolean deleted;

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.MAIL_TEMPLATE_NOT_FOUND.getCode(), ErrorCodeConstants.MAIL_TEMPLATE_NOT_FOUND.getMessage());
        }
    }

    @Override
    public void isDisable() {
        if (StatusEnum.DISABLE.getValue().equals(this.status)) {
            throw new BizException(ErrorCodeConstants.MAIL_TEMPLATE_STATUS_DISABLE.getCode(), ErrorCodeConstants.MAIL_TEMPLATE_STATUS_DISABLE.getMessage());
        }
    }

    public static MailTemplate create(String name, String code, Long accountId, String nickname, String title, String content, String remark) {
        MailTemplate mailTemplate = new MailTemplate();
        mailTemplate.setAccountId(accountId)
                .setTemplateName(name)
                .setTemplateCode(code)
                .setTemplateNickname(nickname)
                .setTemplateTitle(title)
                .setTemplateContent(content)
                .setTemplateParams(mailTemplate.getParamsFromContent(content))
                .setRemark(remark);
        return mailTemplate;
    }

    public static MailTemplate update(Long id, String name, String code, Long accountId, String nickname, String title, String content, String remark) {
        MailTemplate mailTemplate = new MailTemplate();
        mailTemplate.setId(id)
                .setAccountId(accountId)
                .setTemplateName(name)
                .setTemplateCode(code)
                .setTemplateNickname(nickname)
                .setTemplateTitle(title)
                .setTemplateContent(content)
                .setTemplateParams(mailTemplate.getParamsFromContent(content))
                .setRemark(remark);
        return mailTemplate;
    }

    /**
     * 从消息模板内容中提取参数({param})
     *
     * @param templateContent 消息模板内容
     * @return
     */
    public List<String> getParamsFromContent(String templateContent) {
        return ReUtil.findAllGroup1(RegexPatternConstants.PATTERN_PARAMS, templateContent);
    }

    /**
     * 生成邮件标题
     * @param templateTitle 消息模板内容
     * @param params 参数值
     * @return
     */
    public String formatTemplateTitle(String templateTitle, Map<String, Object> params) {
        return StrUtil.format(templateTitle, params);
    }

    /**
     * 生成邮件内容
     * @param templateContent 消息模板内容
     * @param params 参数值
     * @return
     */
    public String formatTemplateContent(String templateContent, Map<String, Object> params) {
        return StrUtil.format(templateContent, params);
    }
}
